package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.dto.DeviceRecordDTO;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.domain.entity.DeviceRecord;
import com.hqully.labmanage.domain.vo.DeviceRecordVO;
import com.hqully.labmanage.mapper.DeviceRecordMapper;
import com.hqully.labmanage.service.DeviceRecordService;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[deviceRecord]的操作Service
 * </p>
 *
 * @entity {@link DeviceRecord}
 * @date 2022-10-08 10:05:23 <br>
 * @author hqully <br>
 * @version 1.0
 */
@Slf4j
@Service
public class DeviceRecordServiceImpl implements DeviceRecordService {


    @Resource
    private DeviceRecordMapper deviceRecordMapper;

    @Override
    public PageInfo<DeviceRecordVO> pageDeviceRecords(PageDTO page, DeviceRecordDTO deviceRecordDTO) {
        try {
            PageHelper.startPage(page);
            List<DeviceRecordVO> deviceRecordList = deviceRecordMapper.listDeviceRecords(deviceRecordDTO);
            return new PageInfo<>(deviceRecordList);
        } catch (Exception e) {
            log.error("PAGE [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备使用记录分页列表失败");
        }
    }

    @Override
    public List<DeviceRecordVO> listDeviceRecords(DeviceRecordDTO deviceRecordDTO) {
        try {
            return deviceRecordMapper.listDeviceRecords(deviceRecordDTO);
        } catch (Exception e) {
            log.error("LIST [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备使用记录列表失败");
        }
    }

    @Override
    public int addDeviceRecord(DeviceRecordDTO deviceRecordDTO) {
        DeviceRecord deviceRecord = new DeviceRecord();
        BeanUtils.copyProperties(deviceRecordDTO, deviceRecord);

        int status;
        try {
            status = deviceRecordMapper.addDeviceRecord(deviceRecord);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceRecordDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加设备使用记录失败\n原因: 设备使用记录id已存在");
        } catch (Exception e) {
            log.error("ADD [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加设备使用记录失败");
        }
        return status;
    }

    @Override
    public int updateDeviceRecord(DeviceRecordDTO deviceRecordDTO) {
        DeviceRecord deviceRecord = new DeviceRecord();
        BeanUtils.copyProperties(deviceRecordDTO, deviceRecord);

        int status;
        try {
            status = deviceRecordMapper.updateDeviceRecord(deviceRecord);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [deviceRecord] record not exist");
            }
        }catch (DAOException e){
            log.warn("UPDATE [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceRecordDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新设备使用记录失败\n原因: 设备使用记录不存在");
        }
        catch (Exception e) {
            log.error("UPDATE [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新设备使用记录失败");
        }
        return status;
    }

    @Override
    public int deleteDeviceRecordById(String id) {
        int status;
        try {
            status = deviceRecordMapper.deleteDeviceRecordById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [deviceRecord] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除设备使用记录失败\n原因: 设备使用记录不存在");
        } catch (Exception e) {
            log.error("DELETE [deviceRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除设备使用记录失败");
        }
        return status;
    }


}



